package visualInterface;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.Border;

public class MainFrame extends JFrame {

	private JMenuBar menubar = new JMenuBar();

	private JMenu fileMenu = new JMenu("File");
	private JMenu viewMenu = new JMenu("View");

	private JMenuItem newSeason = new JMenuItem("New season");
	private JMenuItem newGameWeek = new JMenuItem("New game week");

	private JMenuItem viewSeason = new JMenuItem("Season");
	private JMenuItem viewgameWeek = new JMenuItem("Game week");
	private JMenuItem viewMatch = new JMenuItem("Match");

	ArrayList<VisualGames> gamesList = new ArrayList<VisualGames>();
	ArrayList<JPanel> panelList = new ArrayList<JPanel>();
	JTextArea clasament = new JTextArea();

	BackgroungImages mainPanel;
	JScrollPane upperPanel = new JScrollPane();
	JPanel centerUpperPanel = new JPanel();
	JScrollPane lowerPanel = new JScrollPane();
	JPanel centerLowerPanel = new JPanel();

	GridLayout mainPanelLayout = new GridLayout(2, 1);
	GridLayout centerUpperPanelLayout = new GridLayout(10, 1);

	private void addVisualGame(JPanel panel, VisualGames game) {
		GridBagConstraints c = new GridBagConstraints();
		GridBagConstraints c1 = new GridBagConstraints();
		c.insets = new Insets(3, 3, 3, 3);
		// c1.gridy=i;
		c.gridx = 0;
		// c.gridy = i;
		panel.add(game.homeTeam, c);

		c.gridx = 1;
		// c.gridy = i;
		panel.add(game.separator1, c);

		c.gridx = 2;
		// c.gridy = i;
		panel.add(game.awayTeam, c);

		c.gridx = 3;
		// c.gridy = i;
		panel.add(game.separator2, c);

		c.gridx = 4;
		// c.gridy = i;
		panel.add(game.homeTeamGoal, c);

		c.gridx = 5;
		// c.gridy = i;
		panel.add(game.separator3, c);

		c.gridx = 6;
		// c.gridy = i;
		panel.add(game.awayTeamGoal, c);

		c.gridx = 7;
		// c.gridy = i;
		panel.add(game.halfTimeInit, c);

		c.gridx = 8;
		// c.gridy = i;
		panel.add(game.halfTimeHomeTeamGoal, c);

		c.gridx = 9;
		// c.gridy = i;
		panel.add(game.separator4, c);

		c.gridx = 10;
		// c.gridy = i;
		panel.add(game.halfTimeAwayTeamGoal, c);

		c.gridx = 11;
		// c.gridy = i;
		panel.add(game.halfTimeEnd, c);

		c.gridx = 12;
		// c.gridy = i;
		panel.add(game.detailsButton, c);
		// panel1.add(panel,c1);
	}

	public JMenu getFileMenu() {
		return fileMenu;
	}

	public ArrayList<VisualGames> getGamesList() {
		return gamesList;
	}

	public JMenuItem getNewGameWeek() {
		return newGameWeek;
	}

	public JMenuItem getNewSeason() {
		return newSeason;
	}

	public JMenuItem getViewgameWeek() {
		return viewgameWeek;
	}

	public JMenuItem getViewMatch() {
		return viewMatch;
	}

	public JMenu getViewMenu() {
		return viewMenu;
	}

	public JMenuItem getViewSeason() {
		return viewSeason;
	}

	public void hideFrame() {
		setVisible(false);
	}

	public void setFileMenu(JMenu fileMenu) {
		this.fileMenu = fileMenu;
	}

	public void setFrame() {
		Dimension frameDimension = Toolkit.getDefaultToolkit().getScreenSize();
		setSize(frameDimension.width, frameDimension.height - 45);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setDefaultLookAndFeelDecorated(true);

		setJMenuBar(menubar);
		upperPanel.setBorder(BorderFactory.createEmptyBorder());
		setFrameComponent();
		centerLowerPanel.add(clasament);

		clasament
				.append("asdassadasfasdfasd hf bsadliasdi asoiasoi asi saoi asoig asoi asoi aosig aosi oaisoiasoiasioqs oisag oqigd");
		clasament.setLineWrap(false);
		// clasament.setFont(new Font("TimesNewRoman", Font.PLAIN,60));
		clasament.setOpaque(false);
		lowerPanel.setBorder(BorderFactory.createEmptyBorder());
		centerLowerPanel.setOpaque(false);
		lowerPanel.getViewport().add(centerLowerPanel);
		lowerPanel.getViewport().setOpaque(false);
		lowerPanel.setOpaque(false);
		mainPanel.add(lowerPanel);
		setContentPane(mainPanel);

	}

	public void setFrameComponent() {
		mainPanel = new BackgroungImages("soccer.jpg", this);

		mainPanel.setLayout(mainPanelLayout);
		centerUpperPanel.setLayout(centerUpperPanelLayout);

		menubar.add(fileMenu);
		fileMenu.add(newSeason);
		newSeason.setActionCommand("newSeason");
		fileMenu.add(newGameWeek);

		menubar.add(viewMenu);
		viewMenu.add(viewSeason);
		viewMenu.add(viewgameWeek);
		viewMenu.add(viewMatch);
		for (int i = 0; i < 9; i++) {
			VisualGames game = new VisualGames();
			JPanel panel = new JPanel();
			GridBagLayout layout = new GridBagLayout();

			panel.setLayout(layout);
			panel.setOpaque(false);
			gamesList.add(game);
			panelList.add(panel);

			addVisualGame(panel, game);

			centerUpperPanel.add(panel);
		}
		centerUpperPanel.setOpaque(false);
		upperPanel.getViewport().add(centerUpperPanel);
		upperPanel.setOpaque(false);
		upperPanel.getViewport().setOpaque(false);
		mainPanel.add(upperPanel);

	}

	public void setGamesList(ArrayList<VisualGames> gamesList) {
		this.gamesList = gamesList;
	}

	public void setNewGameWeek(JMenuItem newGameWeek) {
		this.newGameWeek = newGameWeek;
	}

	public void setNewSeason(JMenuItem newSeason) {
		this.newSeason = newSeason;
	}

	public void setViewgameWeek(JMenuItem viewgameWeek) {
		this.viewgameWeek = viewgameWeek;
	}

	public void setViewMatch(JMenuItem viewMatch) {
		this.viewMatch = viewMatch;
	}

	public void setViewMenu(JMenu viewMenu) {
		this.viewMenu = viewMenu;
	}

	public void setViewSeason(JMenuItem viewSeason) {
		this.viewSeason = viewSeason;
	}

	public void showFrame() {
		setVisible(true);
	}
}
